package top.xcxdl.rural.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import top.xcxdl.rural.entity.RuralCourse;
import top.xcxdl.rural.entity.RuralProduct;
import top.xcxdl.rural.entity.RuralUser;
import top.xcxdl.rural.mapper.RuralProductMapper;
import top.xcxdl.rural.service.IRuralProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.xcxdl.rural.utils.ImageAddress;
import top.xcxdl.rural.utils.JWTUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-05-06
 */
@Log4j2
@Service
public class RuralProductServiceImpl extends ServiceImpl<RuralProductMapper, RuralProduct> implements IRuralProductService {

    @Override
    public PageInfo<RuralProduct> getList(Integer pageNo, Integer pageSize, Integer productStatus,Integer productType, Integer authorType, String key) {
        PageHelper.startPage(pageNo, pageSize);
        List<RuralProduct> list = baseMapper.getList(productStatus,productType, authorType, key);
        list.forEach(product -> {
            if (product.getResourcesString() != null) {
                //将sql返回的url字符串数组转换为数组
                String[] resourcesArray = product.getResourcesString().split(",");
                //将数组转换为list
                List<String> resourcesList = new ArrayList<>();
                for (String url : resourcesArray) {
                    url = ImageAddress.getAllPath(url);
                    resourcesList.add(url);
                }
                //将list赋值给product的resources
                product.setResourcesArray(resourcesList);
                product.setResourcesString(null);
            }
        });
        return new PageInfo<>(list);
    }

    @Override
    public int doEdit(RuralProduct product) {
        product.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        if (product.getProductId() == null) {
            //新增
            product.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
            log.info("{}{}（ID为{}）发布了一个产品信息",JWTUtils.getAuthorTypeName(),JWTUtils.getAuthorName(),JWTUtils.getAuthorId());
            return baseMapper.insert(product);
        }else{
            //修改
            return baseMapper.updateById(product) + 1;
        }
    }

    @Override
    public int delete(List<Integer> ids) {
        QueryWrapper<RuralProduct> wrapper = new QueryWrapper<>();
        wrapper.in("product_id", ids);
        log.info("{}{}（ID为{}）：删除了{}个产品，id为：{}",JWTUtils.getAuthorTypeName(),JWTUtils.getAuthorName(), JWTUtils.getAuthorId(),ids.size(),ids);
        return baseMapper.delete(wrapper);
    }

    @Override
    public PageInfo<RuralProduct> getProductListByUser(Integer pageNo, Integer pageSize, String key) {
        PageHelper.startPage(pageNo, pageSize);
        List<RuralProduct> list = baseMapper.getProductListByUser(key);
        list.forEach(product -> {
            if (product.getResourcesString() != null) {
                //将sql返回的url字符串数组转换为数组
                String[] resourcesArray = product.getResourcesString().split(",");
                //将数组转换为list
                List<String> resourcesList = new ArrayList<>();
                for (String url : resourcesArray) {
                    url = ImageAddress.getAllPath(url);
                    resourcesList.add(url);
                }
                //将list赋值给product的resources
                product.setResourcesArray(resourcesList);
                product.setResourcesString(null);
            }
        });
        return new PageInfo<>(list);
    }

    @Override
    public RuralProduct getDetail(Integer productId) {
        RuralProduct product = baseMapper.getDetail(productId);
        if (product.getResourcesString() != null) {
            //将sql返回的url字符串数组转换为数组
            String[] resourcesArray = product.getResourcesString().split(",");
            //将数组转换为list
            List<String> resourcesList = new ArrayList<>();
            for (String url : resourcesArray) {
                url = ImageAddress.getAllPath(url);
                resourcesList.add(url);
            }
            //将list赋值给product的resources
            product.setResourcesArray(resourcesList);
            product.setResourcesString(null);
        }
        return product;
    }

    @Override
    public PageInfo<RuralProduct> getProductListByUserId(Integer pageNo, Integer pageSize, Integer authorId, Integer authorType, String key) {
        PageHelper.startPage(pageNo, pageSize);
        List<RuralProduct> list = baseMapper.getProductListByUserId(authorId,authorType,key);
        list.forEach(product -> {
            if (product.getResourcesString() != null) {
                //将sql返回的url字符串数组转换为数组
                String[] resourcesArray = product.getResourcesString().split(",");
                //将数组转换为list
                List<String> resourcesList = new ArrayList<>();
                for (String url : resourcesArray) {
                    url = ImageAddress.getAllPath(url);
                    resourcesList.add(url);
                }
                //将list赋值给product的resources
                product.setResourcesArray(resourcesList);
                product.setResourcesString(null);
            }
        });
        return new PageInfo<>(list);
    }

    @Override
    public List<RuralProduct> getProductListBySearch(String key) {
        QueryWrapper<RuralProduct> wrapper = new QueryWrapper<>();
        wrapper.like("product_name",key);
        return baseMapper.selectList(wrapper);
    }

}
